﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Chart;
using System.Linq;

namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UIFWB19 : UIWB19
    {
        public int pos = 1;
     

        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length == 0)
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 4);
            ERVER = int.Parse(EXAUF.Substring(12));
            PM["RWKRS"] = KRS;
            PM["RWLFD"] = LFD;
            PM["RWPOS"] = POS;
            PM["ERVER"] = ERVER;
            SResult = TMV.LOADERVER(PM);
            if(IsCanNew)
            {
                MergeData(SResult, true);
            }
           else
            {
                MergeData(SResult, false);
            }
            SetUpChart();
           
            if (TBMain.Rows.Count == 0)
            {
                pos = 0;
            }
            else
            {
                var rm = (from DataRow row in TBMain.AsEnumerable()
                          select Convert.ToInt16(row["WBFFPOS"].ToString()))
                 .Max();
                pos = int.Parse(rm.ToString());
            }
            pos++;
            if (!IsAuto)
                return;
            PM["ERVER"] = ERVER;
            PM["FPOS"] = CPCS * 100 + pos;
            PM["QPKT"] = 0;
            TMV.NEWERROR(PM);
            vwMaster.View.MoveCurrentToFirst();
            base.OnEXAUFChg(vlaue);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
        }
        private void KEYUP(object sender, KeyEventArgs e)
        {
            QueryCommand(sender, e);
        }
      
       
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
                if (TMV.WBFLGVON < 0)
                {
                    TMV.WBFLGVON = CMTR;
                    TMV.WBFLGBIS = CMTR;
                }
                dwNFError = null;
                //触发坯布界面接收一个新的疵点
                if(dwMaster!=null)
                {
                    dwData = TBMain.Clone().NewRow();
                    dwData.ItemArray = dwMaster.ItemArray;
                    dwNFError = dwData;
                    /*string num = EKEY.Substring(20,2);
                    if (num != "00"&&num.Trim().Length!=0)
                    {
                        dwMaster["WBFQPKT"] = num.TrimStart(new char[]{'0'});
                    }*/
                    TMV.UpdateWBFEHL(dwMaster, 0);
                    
                } 
                pos++;
                PM["RWKRS"] = KRS;
                PM["RWLFD"] = LFD;
                PM["RWPOS"] = POS;
                PM["ERVER"] = ERVER;
                PM["FPOS"] = CPCS * 100 + pos;
                PM["QPKT"] = 0;
                TMV.NEWERROR(PM);
                IsNew = false;
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
                IsNew = true;//这个会触发坯布验布的程序会跟着插入新纪录
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex.Message);
            }
        }
         
    }
}
